package edu.just.pet.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import edu.just.pet.entity.Cat;
import edu.just.pet.service.CatService;
import edu.just.pet.system.Constants;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("cat")
public class CatController {
    @Autowired
    private CatService catService;

    @RequestMapping("index.do")
    public ModelAndView index() {
        Map<String, Object> result = new HashMap<>();

        List<Cat> catList = catService.getCatListByPage(new Cat());

        result.put(Constants.RETURN_DATA, catList);
        result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
        result.put(Constants.RETURN_MSG, "请求成功");

        return new ModelAndView("/back/pets/cat_index", result);
    }

    @RequestMapping("index1.do")
    public ModelAndView index1() {
        Map<String, Object> result = new HashMap<>();

        List<Cat> catList = catService.getCatListByPage(new Cat());

        result.put(Constants.RETURN_DATA, catList);
        result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
        result.put(Constants.RETURN_MSG, "请求成功");

        return new ModelAndView("/front/catdisplaypage", result);
    }
    /**
     * 获得宠物列表
     * @param cat
     * @return
     */
    @RequestMapping("getCatList.do")
    @ResponseBody
    public Map index(Cat cat){

//        System.out.println(cat);
        Map<String,Object> result =new HashMap<>();

        List<Cat> catList=catService.getCatListByPage(cat);
        PageInfo<Cat> pageInfo=new PageInfo<>(catList);

        result.put(Constants.RETURN_DATA, pageInfo.getList());
        result.put(Constants.RETURN_COUNT, pageInfo.getTotal());
        result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
        result.put(Constants.RETURN_MSG, "请求成功");

        return result;
    }

    /**
     * 删除宠物
     * @return
     */
    @RequestMapping("deleteCat.do")
    @ResponseBody
    public Map<String, Object> deleteCat(Cat cat) {
        Map<String, Object> result = new HashMap<>();

        if(catService.deleteCat(cat) > 0) {
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, "删除成功");
        }else {
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, "删除成功");
        }
        return result;
    }

    /**
     * 批量删除
     * @param catList
     * @return
     */
    @RequestMapping("deleteCatList.do")
    @ResponseBody
    public Map<String, Object> deleteCatList(@RequestBody List<Cat> catList) {
        Map<String, Object> result = new HashMap<>();

        if(catService.deleteCatList(catList) > 0) {
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, "删除成功");
        }else {
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, "删除成功");
        }
        return result;
    }

    @RequestMapping("toAdd.do")
    public ModelAndView toAdd(Cat cat) {

        Map<String, Object> result = new HashMap<>();

        // 调用service层
        result.put(Constants.RETURN_CODE, "1");
        result.put(Constants.RETURN_MSG, "请求成功");

        return new ModelAndView("/back/pets/cat_add", result);
    }

    /**
     * 添加宠物
     * @param cat
     * @return
     */
    @RequestMapping("addCat.do")
    @ResponseBody
    public Map<String, Object> addCat(Cat cat) {

        Map<String, Object> result = new HashMap<>();

        System.out.println(cat);
        // 校验参数
        if(StrUtil.isEmpty(cat.getName()) || cat.getAge() == null
                || cat.getSex() == null) {
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, Constants.PARAM_ERROR);

            return result;
        }
        try {
            // 业务层写在try catch块中
            catService.addCat(cat);
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, Constants.MSG_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, Constants.MSG_ERROR);
        }
        return result;
    }

    @RequestMapping("adop.do")
    @ResponseBody
    public Map<String,Object> adop(Cat cat){
        Map<String, Object> result = new HashMap<>();
        if(catService.adop(cat) > 0) {
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, "认养成功");
        }else {
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, "认养失败");
        }
        return result;
    }

    @RequestMapping("toUpdate.do")
    @ResponseBody
    public ModelAndView toUpdate(Cat cat){
        Map<String, Object> result = new HashMap<>();

        // 调用service层
        result.put(Constants.RETURN_DATA, catService.getCatInfoById(cat));
        result.put(Constants.RETURN_CODE, "1");
        result.put(Constants.RETURN_MSG, "请求成功");

        return new ModelAndView("/back/pets/cat_update", result);
    }

    @RequestMapping("updateCat.do")
    @ResponseBody
    public Map<String, Object> updateCat(Cat cat) {
        System.out.println(cat);
        Map<String, Object> result = new HashMap<>();
        // 校验参数
        if(StrUtil.isEmpty(cat.getName()) || cat.getAge() == null
                || cat.getSex() == null) {

            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, Constants.PARAM_ERROR);
            System.out.println("nulllll");
            return result;
        }
        if (cat.getIsAdop()==0)
            cat.setAdopter(null);

        if(catService.updateCat(cat)>0) {
            result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
            result.put(Constants.RETURN_MSG, "修改成功");
            System.out.println("success");
        }else {
            result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
            result.put(Constants.RETURN_MSG, "修改失败");
            System.out.println("failed");
        }
        return result;
    }

    @RequestMapping("toDetail.do")
    @ResponseBody
    public ModelAndView toDetail(Cat cat){
        Map<String,Object> result=new HashMap<>();
        System.out.println(cat);
        // 调用service层
        result.put("type",1);
        result.put(Constants.RETURN_DATA, catService.getCatInfoById(cat));
        result.put(Constants.RETURN_CODE, "1");
        result.put(Constants.RETURN_MSG, "请求成功");

        return new  ModelAndView("/front/detailpage",result);
    }



    @RequestMapping("upload.do")
    @ResponseBody
    public Map<String,Object> upload(@RequestParam(value = "file") CommonsMultipartFile file){

        String firstName=file.getOriginalFilename();
        //后缀
        String type= FilenameUtils.getExtension(firstName);
        String dateName= DateTime.now().toString("yyyyMMddhhmmss");
        String rootRoad="C:\\Users\\23570\\Desktop\\jsp\\JSP实践\\pet\\src\\main\\webapp\\animal\\";
        String rootRoad1="/image/";
        String newFileName=dateName;
        String newRoad=rootRoad+newFileName+"."+type;
        String newRoad1=rootRoad1+newFileName+"."+type;
        File file1=new File(newRoad);

        try{
            file.transferTo(file1);
        }catch (IOException e){
            e.printStackTrace();
        }
        Map<String,Object> result=new HashMap<>();
        result.put(Constants.RETURN_CODE,1);
        result.put(Constants.RETURN_MSG,"保存成功");
        result.put("src",newRoad1);
        return result;
    }
}
